/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package interfaz;

import clases.ClsDatabase;
import clases.ClsMetodos;
import javax.swing.JOptionPane;

/**
 *
 * @author mll
 */
public class dlgIndice extends javax.swing.JDialog {

    private ClsMetodos metodos = new ClsMetodos();
    private ClsDatabase bd = new ClsDatabase();

    /**
     * Creates new form dlgIndice
     */
    public dlgIndice(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        iniciar();
    }

    public void iniciar() {
        setLocationRelativeTo(null);//Centramos la ventana
        metodos._iconJDialog(this);
        txtBuscarCategoria.setText("");
        txtBuscarCategoria.requestFocus();
//        metodos.tablaOcultarcolumnas(tblCategoria, new int[]{0});
        metodos.tablaLongitudColumnas(tblCategoria, new int[]{20, 500});
        _cargarTabla_CategoriaInsumo();
    }

    public void _cargarTabla_CategoriaInsumo() {
        String nombreBuscar = txtBuscarCategoria.getText().toString();
        metodos.tablaLlenaSql_defecto(tblCategoria,
                "*",
                "FROM insumo_indice "
                + "WHERE descripcion like '%" + nombreBuscar + "%' "
                + "ORDER BY idinsumo_indice");
    }

    public void EliminarCategoriaInsumo() {
        int fila = tblCategoria.getSelectedRow();//Obtenemos la fila seleccionada
        if (fila != -1) {
            //Confirmamos la eliminacion
            if (JOptionPane.showConfirmDialog(null, "Está seguro que desea eliminar el Índice unificado seleccionado", "Mensaje", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == 0) {
                String id = tblCategoria.getValueAt(fila, 0).toString();
                boolean seRepite = bd.verif_repite("SELECT estado FROM insumo WHERE idinsumo_indice='" + id + "'");
                if (!seRepite) {
                    boolean seElimino = bd.delete("insumo_indice", "idinsumo_indice='" + id + "'");
                    if (seElimino) {
                        _cargarTabla_CategoriaInsumo();
                        //JOptionPane.showMessageDialog(null, "Se elimino la Categoría con exito");
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "No se puede eliminar esta Índice unificado, esta siendo usado");
                }
            }
        } else {
            JOptionPane.showMessageDialog(null, "Seleccione un Índice Unificado");
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnAgregar_Categoria = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        tblCategoria = new javax.swing.JTable();
        btnEditar_Categoria = new javax.swing.JButton();
        btnQuitar_Categoria = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        txtBuscarCategoria = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Índice Unificado");

        jPanel1.setBackground(new java.awt.Color(225, 235, 247));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnAgregar_Categoria.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/icon/principales/peque/agregar.png"))); // NOI18N
        btnAgregar_Categoria.setText("Crear");
        btnAgregar_Categoria.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregar_CategoriaActionPerformed(evt);
            }
        });
        jPanel1.add(btnAgregar_Categoria, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 400, 90, -1));

        tblCategoria.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "N°", "Nombre"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblCategoria.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblCategoria.getTableHeader().setReorderingAllowed(false);
        tblCategoria.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblCategoriaMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(tblCategoria);

        jPanel1.add(jScrollPane3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 91, 570, 290));

        btnEditar_Categoria.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/icon/principales/peque/editar.png"))); // NOI18N
        btnEditar_Categoria.setText("Editar");
        btnEditar_Categoria.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditar_CategoriaActionPerformed(evt);
            }
        });
        jPanel1.add(btnEditar_Categoria, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 400, 90, -1));

        btnQuitar_Categoria.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/icon/principales/peque/eliminar.png"))); // NOI18N
        btnQuitar_Categoria.setText("Eliminar");
        btnQuitar_Categoria.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQuitar_CategoriaActionPerformed(evt);
            }
        });
        jPanel1.add(btnQuitar_Categoria, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 400, 110, -1));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Buscar"));
        jPanel2.setOpaque(false);
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        txtBuscarCategoria.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtBuscarCategoriaKeyReleased(evt);
            }
        });
        jPanel2.add(txtBuscarCategoria, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 30, 420, -1));

        jLabel10.setText("Nombre:");
        jPanel2.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 30, -1, 20));

        jPanel1.add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 570, 70));

        jLabel11.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/icon/principales/fondo_dialog.jpg"))); // NOI18N
        jPanel1.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 450, 330));

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/icon/principales/peque/salir.png"))); // NOI18N
        jButton1.setText("Salir");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(500, 400, -1, -1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 611, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 453, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAgregar_CategoriaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregar_CategoriaActionPerformed
        dlg_CategoriaInsumo dialogC = new dlg_CategoriaInsumo(new javax.swing.JFrame(), true, this, tblCategoria);

        dialogC.setVisible(true);
    }//GEN-LAST:event_btnAgregar_CategoriaActionPerformed

    private void txtBuscarCategoriaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtBuscarCategoriaKeyReleased
        _cargarTabla_CategoriaInsumo();
    }//GEN-LAST:event_txtBuscarCategoriaKeyReleased

    private void tblCategoriaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblCategoriaMouseClicked
        if (evt.getClickCount() == 2) {
            dlg_CategoriaInsumo dialogC = new dlg_CategoriaInsumo(new javax.swing.JFrame(), true, this, tblCategoria);
            dialogC.Editar();
        }
    }//GEN-LAST:event_tblCategoriaMouseClicked

    private void btnEditar_CategoriaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditar_CategoriaActionPerformed
        dlg_CategoriaInsumo dialogC = new dlg_CategoriaInsumo(new javax.swing.JFrame(), true, this, tblCategoria);
        dialogC.Editar();
    }//GEN-LAST:event_btnEditar_CategoriaActionPerformed

    private void btnQuitar_CategoriaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQuitar_CategoriaActionPerformed
        EliminarCategoriaInsumo();
    }//GEN-LAST:event_btnQuitar_CategoriaActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(dlgIndice.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(dlgIndice.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(dlgIndice.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(dlgIndice.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                dlgIndice dialog = new dlgIndice(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar_Categoria;
    private javax.swing.JButton btnEditar_Categoria;
    private javax.swing.JButton btnQuitar_Categoria;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable tblCategoria;
    private javax.swing.JTextField txtBuscarCategoria;
    // End of variables declaration//GEN-END:variables
}
